/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.kj.entity.KjActivity;
import com.jeesite.modules.kj.entity.KjCity;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.utils.SystemLogUtil;
import com.jeesite.modules.sys.entity.Log;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.kj.entity.KjActivityComment;
import com.jeesite.modules.kj.service.KjActivityCommentService;

/**
 * kj_activity_commentController
 * @author hzy
 * @version 2021-07-12
 */
@Controller
@RequestMapping(value = "${adminPath}/kj/kjActivityComment")
public class KjActivityCommentController extends BaseController {

	@Autowired
	private KjActivityCommentService kjActivityCommentService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public KjActivityComment get(String id, boolean isNewRecord) {
		return kjActivityCommentService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("kj:kjActivityComment:view")
	@RequestMapping(value = {"list", ""})
	public String list(KjActivityComment kjActivityComment, Model model) {
		model.addAttribute("kjActivityComment", kjActivityComment);
		return "modules/kj/kjActivityCommentList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("kj:kjActivityComment:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<KjActivityComment> listData(KjActivityComment kjActivityComment,KjCity kjCity,String memberName, HttpServletRequest request, HttpServletResponse response) {
		kjActivityComment.setPage(new Page<>(request, response));
		kjActivityComment.getKjActivity().setStatus(KjActivityComment.STATUS_NORMAL);
		KjMember kjMember = new KjMember();
		kjMember.setName(memberName);
		kjActivityComment.setKjMember(kjMember);
		KjActivity kjActivity = new KjActivity();
		kjActivity.setKjCity(kjCity);
		kjActivityComment.setKjActivity(kjActivity);
		Page<KjActivityComment> page = kjActivityCommentService.findPage(kjActivityComment);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("kj:kjActivityComment:view")
	@RequestMapping(value = "form")
	public String form(KjActivityComment kjActivityComment, Model model) {
		model.addAttribute("kjActivityComment", kjActivityComment);
		return "modules/kj/kjActivityCommentForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("kj:kjActivityComment:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated KjActivityComment kjActivityComment, HttpServletRequest request) {
		KjActivityComment oldData = kjActivityCommentService.get(kjActivityComment.getId());
		if(!kjActivityComment.getProcessStatus().equals(oldData.getProcessStatus())){
			String commentType = kjActivityComment.getCommentType().equals("ACTIVITY") ? "活动" : "项目";
			if(kjActivityComment.getProcessStatus().equals(KjActivityComment.ACCEPT)){
				SystemLogUtil.insertAdminOperate(UserUtils.getUser(), commentType + "评论审核", Log.TYPE_UPDATE, "审核通过", kjActivityComment.getKjMember().getName() + "评论的" + "\"" + kjActivityComment.getActivityName() + "\"", request);
			}else{
				SystemLogUtil.insertAdminOperate(UserUtils.getUser(), commentType + "评论审核", Log.TYPE_UPDATE, "审核未通过", kjActivityComment.getKjMember().getName() + "评论的" + "\"" + kjActivityComment.getActivityName() + "\"", request);
			}
		}
		kjActivityCommentService.save(kjActivityComment);
		return renderResult(Global.TRUE, text("保存活动评论成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("kj:kjActivityComment:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(KjActivityComment kjActivityComment) {
		kjActivityCommentService.delete(kjActivityComment);
		return renderResult(Global.TRUE, text("删除活动评论成功！"));
	}

	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("kj:kjActivityComment:view")
	@RequestMapping(value = "kjActivityCommentSelect")
	public String empUserSelect(KjActivityComment kjActivityComment, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("KjActivityComment", kjActivityComment);
		return "modules/kj/kjActivityCommentSelect";
	}


	/**
	 * 停用用户
	 * @param comment
	 * @return
	 */
	@RequiresPermissions("kj:kjActivityComment:edit")
	@ResponseBody
	@RequestMapping(value = "disable")
	public String disable(KjActivityComment comment) {
		comment.setProcessStatus(KjActivityComment.REJECT);
		kjActivityCommentService.save(comment);
		return renderResult(Global.TRUE, text("禁用评论成功"));
	}

	/**
	 * 启用用户
	 * @param comment
	 * @return
	 */
	@RequiresPermissions("kj:kjActivityComment:edit")
	@ResponseBody
	@RequestMapping(value = "enable")
	public String enable(KjActivityComment comment) {
		comment.setProcessStatus(KjActivityComment.ACCEPT);
		kjActivityCommentService.save(comment);
		return renderResult(Global.TRUE, text("启用评论成功"));
	}
}
